﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Exe_1.Controllers;
using Rhino.Mocks;

namespace Exe_1.Tests.Controllers
{
    [TestClass]
    public class HomeControllerTest
    {

        [TestMethod]
        public void Url_with_only_lang_is_mapped_correctly()
        {
            MockRepository mocks = new MockRepository();
            //Mock setup
            HttpContextBase mockHttpContext = mocks.DynamicMock<HttpContextBase>();
            HttpRequestBase mockHttpRequest = mocks.DynamicMock<HttpRequestBase>();

            RouteCollection routes = new RouteCollection();
            MvcApplication.RegisterRoutes(routes);

            using (mocks.Record())
            {
                SetupResult.For(mockHttpContext.Request).Return(mockHttpRequest);

                SetupResult.For(mockHttpRequest.AppRelativeCurrentExecutionFilePath)
                    .Return("~/it");
            }

            using (mocks.Playback())
            {
                RouteData routeData = routes.GetRouteData(mockHttpContext);
                Assert.IsNotNull(routeData, "Should have found the route");

                Assert.AreEqual("Home", routeData.Values["Controller"]
                    , "Expected a different controller");

                Assert.AreEqual("Index", routeData.Values["action"]
                    , "Expected a different action");

                Assert.AreEqual("Home", routeData.Values["pageTitle"]
                    , "Expected a different pagetitle");

                Assert.AreEqual("it", routeData.Values["language"]
                    , "Expected a different language");
            }
        }


        [TestMethod]
        public void Index_is_Rendered_with_Italian()
        {
            ExecTest("it");
        }

        [TestMethod]
        public void Index_is_Rendered_with_English()
        {
            ExecTest("en");
        }

        private void ExecTest(string language)
        {
            // Arrange
            HomeController controller = new HomeController();

            // Act
            ViewResult result = controller.Index("Home", language) as ViewResult;

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual("Index", result.ViewName);
        }

        [TestMethod]
        public void Message_is_Correctly_Formatted()
        {
            // Arrange
            HomeController controller = new HomeController();

            // Act
            ViewResult result = controller.Index("BookList", "en") as ViewResult;

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual("BookList EN", result.ViewData["Message"]);
        }

        [TestMethod]
        public void Redirect_To_NotSupported_with_French()
        {
            // Arrange
            HomeController controller = new HomeController();
            controller.TempData = new TempDataDictionary();

            // Act
            RedirectToRouteResult result = controller.Index("Home", "fr")
                as RedirectToRouteResult;

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual("NotSupported", result.RouteValues["action"]);
            Assert.AreEqual("fr", controller.TempData["language"]);
        }






    }
}
